package com.neutech.dao.impl;




import com.neutech.dao.UserDAO;
import com.neutech.entity.User;
import com.neutech.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: UserDAOImpl
 * @Description: 世界很大, 要找的人很远, 还需努力.
 * @author: Jt
 * @date: 21/02/04  09:38
 */
public class UserDAOImpl implements UserDAO {
    @Override
    public List<User> selectAll() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        try{
            conn = DBUtil.getConnection();
            stmt = DBUtil.getstatement(conn);
            String sql = "select id,user_name,user_password,user_phone,user_addr," +
                    "user_createtime,user_updatetime,user_status from t_user";
            rs = DBUtil.getResultSet(stmt, sql);
            while(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("user_name"));
                user.setPassword(rs.getString("user_password"));
                user.setPhone(rs.getString("user_phone"));
                user.setAddress(rs.getString("user_addr"));
                user.setCreateTime(rs.getTimestamp("user_createtime"));
                user.setUpdateTime(rs.getTimestamp("user_updatetime"));
                user.setStatus(rs.getInt("user_status"));

                users.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback(conn);

        }finally {
            DBUtil.close(rs,stmt,conn);
        }
        return users;
    }

    @Override
    public void insert(User user) {
        Connection conn = null;
        PreparedStatement prep = null;
        String sql = "insert into t_user values(null,?,md5(?),?,?,now(),now(),1)";
        try{
            conn = DBUtil.getConnection();
            prep = DBUtil.getPreparedStatement(conn, sql);
            prep.setString(1,user.getName());
            prep.setString(2, user.getPassword());
            prep.setString(3, user.getPhone());
            prep.setString(4, user.getAddress());
            prep.executeUpdate();
        } catch (Exception e){
            e.printStackTrace();
            DBUtil.rollback(conn);
        } finally {
            DBUtil.close(prep, conn);
        }
    }

    @Override
    public void update(User user) {
        Connection conn = null;
        // Statement stmt = null;
        PreparedStatement prep = null;
        String sql = "update t_user set user_name=?, user_password=md5(?),user_phone=?," +
                "user_addr=?,user_updatetime=now() where id = ?";
        try{
            conn = DBUtil.getConnection();
            prep = DBUtil.getPreparedStatement(conn, sql);
            //设置占位符
            prep.setString(1,user.getName());
            prep.setString(2,user.getPassword());
            prep.setString(3,user.getPhone());
            prep.setString(4,user.getAddress());
            prep.setInt(5,user.getId());

            //执行语句
            prep.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback(conn);
        } finally {
            DBUtil.close(prep,conn);
        }
    }

    @Override
    public void delete(Integer id) {
        Connection conn = null;
        PreparedStatement prep = null;
        String sql = "delete from t_user where id=?";
        try{
            conn = DBUtil.getConnection();
            prep = DBUtil.getPreparedStatement(conn, sql);
            prep.setInt(1, id);
            prep.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback(conn);
        } finally {
            DBUtil.close(prep, conn);
        }
    }

    @Override
    public User selectById(Integer id) {
        Connection conn = null;
        PreparedStatement prep = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select id,user_name,user_password,user_phone,user_addr," +
                "user_createtime,user_updatetime,user_status from t_user where id=?";
        try{
            conn = DBUtil.getConnection();
            prep = DBUtil.getPreparedStatement(conn, sql);
            prep.setInt(1, id);
            rs = DBUtil.getResultSet(prep);
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("user_name"));
                user.setPassword(rs.getString("user_password"));
                user.setPhone(rs.getString("user_phone"));
                user.setAddress(rs.getString("user_addr"));
                user.setCreateTime(rs.getTimestamp("user_createtime"));
                user.setUpdateTime(rs.getTimestamp("user_updatetime"));
                user.setStatus(rs.getInt("user_status"));


            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback(conn);

        }finally {
            DBUtil.close(rs,prep,conn);
        }
        return user;
    }
}
